package test;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.opencv_core.Mat;

public class SavePictures {
    final static int speed = 1000; //抓取图片的速度，单位为毫秒
    final static int width = 3840;
    final static int height = 2160;
    final static String rtsp = "rtsp://admin:123456789a@192.168.31.66:554/h264/ch1/main/av_stream";
    public static void main(String[] args)
    {
        try { savePic(); } catch (Exception e) { e.printStackTrace(); }
    }
    public static void savePic() throws FrameGrabber.Exception
    {
        FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(rtsp);
        grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式，不然会丢包很严重
        grabber.setImageWidth(width);
        grabber.setImageHeight(height);
        grabber.start();
        OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
        int ex = 0;
        while (true)
        {
            Frame frame = grabber.grabImage();
            Mat mat = converter.convertToMat(frame);
            opencv_imgcodecs.imwrite("D:\\FFTB\\" + ex + ".png", mat);
            ex++;
            try { Thread.sleep(speed); } catch (InterruptedException e) { e.printStackTrace(); }
        }
    }
}
